<template>
  <div class="departments-container">
    <div class="app-container">
      <el-card v-loading="loading" class="tree-card">
        <treeTool :node-data="company" :is-show="true" />

        <el-tree
          default-expand-all
          :data="departs"
          :props="defaultProps"
        >
          <template #default="{ data }">
            <treeTool
              :node-data="data"
              @del_depts="getDepartments()"
              @add-depts="handleAddDepts"
              @edit-depts="handleEditDepts"
            />
          </template>
        </el-tree>
      </el-card>

      <addTem ref="addTem" :node-data="nodeData" :dep-list="depList" :dialog-visible.sync="showDialog" @add-depts="getDepartments" />
    </div>
  </div>

</template>

<script>
import treeTool from './components/treeTool.vue'
import addTem from './components/add-tem.vue'
import { reqGetDepartments } from '@/api/departments'
import { tranListToTreeData } from '@/utils/index'
export default {
  name: 'Departments',
  components: {
    treeTool,
    addTem
  },

  data() {
    return {
      depList: [],
      loading: false,
      departs: [
        {
          name: '总裁办',
          manager: '曹操',
          children: [{ name: '董事会', manager: '曹丕' }]
        },
        { name: '行政部', manager: '刘备' },
        { name: '人事部', manager: '孙权' }
      ],
      company: { name: '但是昨晚我真的没睡她有限公司', manager: '负责人' },
      defaultProps: {
        label: 'name',
        children: 'children'
      },
      showDialog: false,
      nodeData: {}
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    async getDepartments() {
      this.loading = true
      const { data } = await reqGetDepartments()
      this.company = {
        name: data.companyName,
        manager: '负责人',
        id: ''
      }
      const res = tranListToTreeData(data.depts, '')
      this.departs = res
      this.depList = data.depts

      this.loading = false
    },

    handleClose(flag) {
      this.showDialog = flag
    },
    // 增加方法
    handleAddDepts(nodeData) {
      this.showDialog = true
      this.nodeData = nodeData
    },

    // 编辑方法
    handleEditDepts(nodeData) {
      this.showDialog = true
      this.nodeData = nodeData
      console.log('在编辑中')
      this.$nextTick(() => {
        this.$refs.addTem.getDepartDetail(nodeData.id)
      })
    }
  }

}
</script>

<style lang='scss' scoped>
  .tree-card {
    padding: 30px;
    font-style: 14px;
    .el-tree {
  ::v-deep {
    // 小三角的样式, 去掉默认的小三角的旋转效果
    .el-tree-node__expand-icon.expanded {
      -webkit-transform: rotate(0deg);
      transform: rotate(0deg);
    }
    // 有子节点 且未展开 +
    .el-icon-caret-right:before {
      background: url("~@/assets/common/add-circle.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    // 有子节点 且已展开 -
    .el-tree-node__expand-icon.expanded.el-icon-caret-right:before{
      background: url("~@/assets/common/minus-circle.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    // 没有子节点
    .el-tree-node__expand-icon.is-leaf::before  {
      background: url("~@/assets/common/user-filling.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
  }
}
  }

</style>
